package de.hsowl.tod4a.global.xml;

import java.net.URLDecoder;
import java.net.URLEncoder;

import de.hsowl.tod4a.global.crypto.CryptoUnit;
import de.hsowl.tod4a.global.model.ServerProperty;

public class DataHelper {

	private ServerProperty prop;

	public DataHelper(ServerProperty prop) {
		this.prop = prop;
	}

	/**
	 * Restores a String. Eg. unzipping, decrypting and url decoding
	 * 
	 * @param text
	 * @return
	 */
	public String restoreString(String text) {
		if (text == null) {
			return null;
		}
		text = URLDecoder.decode(text);
		try {
			// if (prop.COMPRESS == true) {
			// text = ZIPUnit.unzip(text);
			// }
			if (prop.ENCRYPT == true) {
				text = CryptoUnit.decrypt(prop.KEY, text, CryptoUnit.AES_128);
			}
		} catch (Exception e) {
		}
		return text;
	}

	/**
	 * Restores a String. Eg. zipping, encrypting and url encoding
	 * 
	 * @param text
	 * @return
	 */
	public String prepareString(String text) {
		if (text == null) {
			return null;
		}
		try {
			if (prop.ENCRYPT == true) {
				text = CryptoUnit.encrypt(prop.KEY, text, CryptoUnit.AES_128);
			}
			// if (prop.COMPRESS == true) {
			// text = ZIPUnit.zip(text);
			// }
		} catch (Exception e) {
		}
		return URLEncoder.encode(text);
	}
}
